package flow.action;

import flow.model.FlowItemPhase;
import flow.model.FlowItemTest;
import m.common.action.ActionMeta;
import m.common.action.RequestMeta;
import m.common.action.VueResult;
import m.html.VueFormResult;
import m.html.VueListResult;
import m.html.element.attr.Align;
import m.html.element.attr.ButtonType;
import m.html.element.other.Param;
import m.html.form.*;
import m.html.list.ListButton;
import m.html.list.SuccessMethod;
import manage.action.ManageAction;

import java.util.List;

@ActionMeta(name="flow/flowItemTest")
public class FlowItemTestAction extends ManageAction {
    private FlowItemTest model;
    private List<FlowItemPhase> list;
    @RequestMeta(params = {"edit"})
    public VueResult toEdit(boolean edit) throws Exception {
        //model表单
        ModelForm form = new ModelForm("项目信息");
        form.addItem(
            FormInput.input("项目编号", "model.itemNum").setRequired(true)
                    .setEdit(edit).setPlaceholder("请输入项目编号"),
            FormInput.input("项目名称", "model.itemName").setRequired(true)
                    .setEdit(edit).setSpan(2).setPlaceholder("请输入项目名称"),
            FormInput.textarea("项目内容", "model.itemContent")
                    .setEdit(edit).setSpan(3).setPlaceholder("请输入项目内容"),
            FormInput.number("项目级别", "model.level","0",0)
                    .setEdit(edit).setPlaceholder("请输入项目级别"),
            FormInput.select("项目状态", "model.itemStatus", "flowItemStatus")
                    .setEdit(edit).setPlaceholder("请选择项目状态"),
            FormInput.number("金额", "model.amount","元")
                    .setEdit(edit).setPlaceholder("请输入金额"),
            FormInput.date("开始时间", "model.startDate")
                    .setEdit(edit).setPlaceholder("请输入开始时间"),
            FormInput.date("结束时间", "model.endDate")
                    .setEdit(edit).setPlaceholder("请输入结束时间"),
            FormHtml.ins("流程状态","{{model.flowRemark}}"),
            FormInput.selectTree("负责部门","model.dutyOrg.oid","manage.model.AdminGroup:C").setRequired(true)
                    .setEdit(edit).setPlaceholder("请选择负责部门"),
            FormInput.select("负责人","model.dutyUser.oid","'manage.model.AdminLogin:'+model.dutyOrg.oid").setRequired(true)
                    .setEdit(edit).setSpan(2).setPlaceholder("请选择负责人"),
            FormInput.selectTree("参加部门", "model.joinOrg", "manage.model.AdminGroup:C",true)
                    .setEdit(edit).setPlaceholder("请选择参加部门"),
            FormInput.selectItem("参加人", "model.joinUser", "'manage.model.AdminLogin:'+model.dutyOrg.oid+','+model.joinOrg",true)
                    .setEdit(edit).setSpan(2).setPlaceholder("请选择参加人"),
            FormHtml.ins("申请人","{{model.applyUser.realname}}"),
            FormHtml.ins("申请部门","{{model.applyOrg.name}}"),
            FormHtml.ins("创建时间","{{model.createDate}}")
        )
        .addData("model",model)
        .addWatch("model.dutyUser.oid","this.flowNextHandler();")
        .addWatch("model.dutyOrg.oid","this.model.dutyUser={};this.flowNextHandler();")
        .addWatch("model.joinOrg","this.model.joinUser=[];")
        .setFlowEvent(//流程方法
            //验证并返回流程所需业务数据
            FormEvent.verify("verifyModel",
                FormJsTest.falseRemind("this.model.itemNum","项目编号不能为空"),
                FormJsTest.falseRemind("this.model.itemName","项目名称不能为空"),
                FormJsTest.falseRemind("this.model.dutyOrg.oid","请选择负责部门"),
                FormJsTest.falseRemind("this.model.dutyUser.oid","请选择负责人")
            ).end("{model:this.model,list:this.phaseList}"),//返回流程所需业务数据
            //设置流程信息
            FormEvent.custom("setFlowInfo",
                FormJsTest.test("this.parentVue.getNextSection().name=='部门审核'","this.parentVue.setUserList([this.model.dutyUser.oid],false);").toString()
            )
        );
        //table表单
        TableForm table=new TableForm("项目阶段").setEdit(edit,true,"{status:'0'}");
        table.addColumn(
            TableColumn.INDEX_COLUMN,
            TableColumn.text("阶段","phaseName").setFixed(Align.LEFT)
                .setEdit(TableEdit.input()),
            TableColumn.text("主体处","mainDept",250,"manage.model.AdminGroup:C")
                .setEdit(TableEdit.selectTree("manage.model.AdminGroup:C")),
            TableColumn.text("负责人","dutyPerson").setDict("manage.model.AdminLogin")
                .setEdit(TableEdit.selectItem("manage.model.AdminLogin")),
            TableColumn.center("要求完成时间","expectDate",130).setDateFormat("yyyy-MM-dd")
                .setEdit(TableEdit.date()),
            TableColumn.number("立项投资","investment",120,"#,##0.00元")
                .setEdit(TableEdit.number("元")),
            TableColumn.center("状态","status",120,"flow.model.FlowItemPhase!status")
                .setEdit(TableEdit.select("flow.model.FlowItemPhase!status"))
        )
        .setData("phaseList",list);

        VueFormResult result=new VueFormResult();
        return result.addForm(form).addForm(table);
    }
    /*
     * 流程项目测试列表
     */
    public VueResult allList() throws Exception {
        VueListResult result=getVueListResult("流程项目测试列表", FlowItemTest.class,actionUrl("allList"));
        result.setFunction(true,false);
        result.setHeight(-1);
        result.addCol(toIndexColumn());
        result.addCol(toTextColumn("项目编号", "itemNum", 100, true));
        result.addCol(toTextColumn("项目名称", "itemName", 100, true));
        result.addCol(toTextColumn("项目内容", "itemContent", 100, true));
        result.addCol(toIntegerColumn("项目级别", "level", 100, true));
        result.addCol(toTextColumn("项目状态", "itemStatus", 100, true).setDictType("flowItemStatus"));
        result.addCol(toNumberColumn("金额", "amount", 100, true));
        result.addCol(toDateColumn("开始时间", "startDate", 100, true));
        result.addCol(toDateColumn("结束时间", "endDate", 100, true));
        result.addCol(toTextColumn("负责部门", "dutyOrg.name", 100, true));
        result.addCol(toTextColumn("负责人", "dutyUser.realname", 100, true));
        result.addCol(toTextColumn("参加部门", "joinOrg", 100, true).setDictType("manage.model.AdminGroup:C"));
        result.addCol(toTextColumn("参加人", "joinUser", 100, true).setDictType("manage.model.AdminLogin"));
        result.addCol(toTextColumn("流程状态", "flowStatus", 100, true).setDictType("flow.model.FlowItemTest!flowStatus"));
        result.addCol(toTextColumn("流程备注", "flowRemark", 100, true));
        result.addCol(toTextColumn("申请人", "applyUser.realname", 100, true));
        result.addCol(toTextColumn("申请部门", "applyOrg.name", 100, true));
        result.addCol(toDateColumn("创建时间", "createDate", 100, true));
        result.addCol(toSpaceColumn("操作", 100,
            ListButton.modal("修改", ButtonType.PRIMARY,"page/base/flow/flowInfo.js",1000, SuccessMethod.REFRESH)
                .addParam("business.oid","oid")
                .addQueryParam(Param.value("edit","true"))
        ).setFixed(Align.RIGHT));
        result.addButton(
            ListButton.modal("新增", ButtonType.PRIMARY,"page/base/flow/flowInfo.js",1000, SuccessMethod.REFRESH)
                .addQueryParam(Param.value("business.busiService","flow.service.FlowItemTestService"))
                .addQueryParam(Param.value("edit","true"))
        );
        //排序 result.addOrder
        //查询条件 result.addCondition
        return result;
    }

    public List<FlowItemPhase> getList() {
        return list;
    }

    public void setList(List<FlowItemPhase> list) {
        this.list = list;
    }

    public FlowItemTest getModel() {
        return model;
    }

    public void setModel(FlowItemTest model) {
        this.model = model;
    }
}
